package com.example.my_add_calendar

import android.app.Activity
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** MyAddCalendarPlugin */
class MyAddCalendarPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : MethodChannel
  private lateinit var context : Context
  private lateinit var activity : Activity

  override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "my_add_calendar")
    channel.setMethodCallHandler(this)
    context = flutterPluginBinding.applicationContext
  }

  @RequiresApi(Build.VERSION_CODES.N)
  override fun onMethodCall(call: MethodCall, result: Result) {
    when (call.method) {
      "getPlatformVersion" -> {
        result.success("Android ${android.os.Build.VERSION.RELEASE}")
      }
      "createEvent" -> {
        val title = call.argument<String>("title")
        val note = call.argument<String>("note")
        val alert = call.argument<List<Int>>("alert")!!
        val beginTime = call.argument<Long>("beginTime")
        val endTime = call.argument<Long>("endTime")
        val eventId = call.argument<String>("eventId")
        val allDay = call.argument<Int>("allDay")!!
        val calendars = Calendars(
          title!!,
          note!!, alert, eventId!!, beginTime!!, endTime!!, allDay
        )
        val resAdd = CalendarProviderUtil.addEvent(activity, calendars)
        result.success(resAdd)
      }

      "deleteEvent" -> {
        val eventId0 = call.argument<String>("eventId")
        val resDel = CalendarProviderUtil.deleteEvent(activity, eventId0!!)
        result.success(resDel)
      }

      "updateEvent" -> {
        val title1 = call.argument<String>("title")
        val note1 = call.argument<String>("note")
        val alert1 = call.argument<List<Int>>("alert")!!
        val beginTime1 = call.argument<Long>("beginTime")
        val endTime1 = call.argument<Long>("endTime")
        val eventId1 = call.argument<String>("eventId")
        val allDay1 = call.argument<Int>("allDay")!!
        val calendars1 = Calendars(
          title1!!,
          note1!!, alert1, eventId1!!, beginTime1!!, endTime1!!, allDay1
        )
        val resUp = CalendarProviderUtil.updateEvent(activity, calendars1)
        result.success(resUp)
      }

      "CheckReadPermission" -> {
        val resRead: Boolean = CalendarProviderUtil.checkCalendarReadPermission(activity)
        result.success(resRead)
      }

      "CheckWritePermission" -> {
        val resWrite: Boolean = CalendarProviderUtil.checkCalendarWritePermission(activity)
        result.success(resWrite)
      }
    }
  }

  override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activity = binding.activity
  }

  override fun onDetachedFromActivityForConfigChanges() {
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
  }

  override fun onDetachedFromActivity() {
  }
}
